<?php
class frm_statistic{
	private $args;
	function __construct($arg){
		$this->args = $arg;
	}
	function getPublish(){
		{ $ptnmng  = new Util\str\PatternManager( $_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "statistic" .'.php'  );} ;
		$statistic = array();
		$mailaddress = $this->args["mail_address"];
		{if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
		if(strlen($mailaddress) > 0){
			$statistic['account'] = array();
			$user =  FunctionData::$static  ->get( "mail_reginfo_" .$mailaddress ) ;
			if($user!=null){
				$userobj = DataUserRegister::swap(ByteBuffer::wrap( $user ) );
				$id =  FunctionData::$static  ->get( "mail_" .$mailaddress ) ;
				if($id != null){
					$statistic['account']['id'] = $id;
				}else{
					$confirm =  FunctionData::$static  ->get( "mail_confirm_" .$mailaddress ) ;
					$confirmobj = DataWaitingConfirm::swap(ByteBuffer::wrap( $confirm ) );
					if($confirm != null){
						$statistic['account']['confirm_code'] = $confirmobj->getCode();
					}
				}
				$statistic['account']['email'] = $mailaddress;
				$statistic['account']['pass_hash'] = implode(array_map("chr",  $userobj->PasswordHashBytes )) ;
				$statistic['account']['secret_key'] = implode(array_map("chr",  $userobj->SecretKeyBytes )) ;
			}
		}
		$ptnglobal = $ptnmng->global;
		$ptn_var = $ptnmng->variable_item;
		$gened_id =  FunctionData::$static  ->get( "nextid"  ) ;
		$new_id =  FunctionData::$static  ->get( "mng_numof_newid"  ) ;
		$reg_user =  FunctionData::$static  ->get( "mng_numof_user_registed"  ) ;
		$confirm_user =  FunctionData::$static  ->get( "mng_numof_user_confirm"  ) ;
		$nof_guest =  FunctionData::$static  ->get( "mng_numof_guest"  ) ;
		if($reg_user == null) $reg_user = 0;
		if($confirm_user == null) $confirm_user = 0;
		if($gened_id == null) $gened_id = 0;
		if($new_id == null) $new_id = 0;
		$ptn_var->variable = "Number Of ID";
		$ptn_var->value = ($gened_id>0?($gened_id - 1):0);
		$ptnglobal->global_items .=  $ptn_var ->publish() ;
		$used_id = ($gened_id - $new_id - 1);
		$ptn_var->variable = "Number Of Used ID";
		$ptn_var->value = ($used_id >= 0?$used_id:0);
		$ptnglobal->global_items .=  $ptn_var ->publish() ;
		$ptn_var->variable = "Number Of New ID";
		$ptn_var->value = $new_id;
		$ptnglobal->global_items .=  $ptn_var ->publish() ;
		$ptn_var->variable = "Number Register User";
		$ptn_var->value = $reg_user;
		$ptnglobal->global_items .=  $ptn_var ->publish() ;
		$ptn_var->variable = "Number Of Confirm User";
		$ptn_var->value = $confirm_user;
		$ptnglobal->global_items .=  $ptn_var ->publish() ;		
		$ptn_var->variable = "Number Of Guest";
		$ptn_var->value = $nof_guest;
		$ptnglobal->global_items .=  $ptn_var ->publish() ;
		$area_manager = FunctionData::readAreasManager();
		$system_areas = $area_manager->getListSystemAreas();
		$statistic['Areas'] = array();
		if(is_array($system_areas) && count($system_areas) > 0){
			$areas = "";
			foreach ($system_areas as $area){
				$areas .= ((strlen($areas)>0)?", ":"").$area;
			}
			$ptn_var->variable = "Areas";
			$ptn_var->value = $areas;
			$ptnglobal->global_items .=  $ptn_var ->publish() ;	
		}
		$group_manager = FunctionData::readGroupsManager();
		$system_groups = $group_manager->getSystemGroups();
		$statistic['Groups'] = array();
		if(is_array($system_groups) && count($system_groups) > 0){
			$groups = "";
			foreach ($system_groups as $group){
				$groups .= ((strlen($groups)>0)?", ":"").$group;
			}
			$ptn_var->variable = "Groups";
			$ptn_var->value = $groups;
			$ptnglobal->global_items .=  $ptn_var ->publish() ;
		}
		$ptn_roletable = $ptnmng->roletable;
		$ptn_div = $ptnmng->sizediv;
		$ptn_row = $ptnmng->row;
		if(is_array($system_areas) && count($system_areas) > 0 && is_array($system_groups) && count($system_groups) > 0){
			$partnum = count($system_areas) + 1;
			$ptn_div->partnum = $partnum;
			$ptn_div->content = " Groups\Areas";
			$ptn_div->classes = "title";
			$ptn_row->cells .=  $ptn_div ->publish() ;
			$ptn_div->classes = "";
			foreach($system_areas as $area){
				$ptn_div->content = $area;
				$ptn_row->cells .=  $ptn_div ->publish() ;
			}
			$ptn_roletable->table_header =  $ptn_row ->publish() ;
			foreach($system_groups as $group){
				$ptn_row->cells = '';
				$ptn_div->content = $group;
				$ptn_row->cells .=  $ptn_div ->publish() ;
				if(is_array($system_areas) && count($system_areas) > 0){
					foreach($system_areas as $area){
						$ptn_div->content = Session::getRole($area,$group);
						$ptn_row->cells .=  $ptn_div ->publish() ;
					}
				}
				$ptn_roletable->rows .=  $ptn_row ->publish() ;
			}
		}
		$ptnapp = $ptnmng->application;
		$ptn_appitem = $ptnmng->appitem;
		$appmng = FunctionData::readAppManager();
		$appids = $appmng->getAppsID();
		$ptn_appitem->ID = "ID";
		$ptn_appitem->title = "Name";
		$ptn_appitem->directory = "Directory";
		$ptn_appitem->secretkey = "Secret Key";
		$ptnapp->apps .=  $ptn_appitem ->publish() ;
		if(is_array($appids)){
			foreach($appids as $appid){
				$app = FunctionData::readApp($appid);
				$app_name = $appmng->getAppName($appid);
				$ptn_appitem->ID = $appid;
				$ptn_appitem->title = $app_name;
				$ptn_appitem->directory = FunctionApp::getAppDirectory($appid);
				$ptn_appitem->secretkey = $app->getSecretKey();
				$ptnapp->apps .=  $ptn_appitem ->publish() ;
			}
		}
		$ptnmng->main->sections .=  $ptnglobal ->publish() ;
		$ptnmng->main->sections .=  $ptn_roletable ->publish() ;
		$ptnmng->main->sections .=  $ptnapp ->publish() ;
		$ptnmng->main->sections .=  $ptnmng->userinfo ->publish() ;
		return  $ptnmng->main ->publish() ;
	}
}
?>